package com.unsiv.helicopter;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

/**
 * <p>
 * 初始化窗体布局
 * </p>
 * 
 * @author fanzhen
 * @time 11:40:01 AM Oct 25, 2011
 * @email fanzhen.meng@gmail.com
 * @qq 474146502
 * @phone 13953109657
 */
public class InitView extends ViewGroup {
	private ImageButton airplane;
	private ImageButton boat;
	private ImageButton tank;
	private ImageButton car;

	public InitView(Context context) {
		super(context);
		initData(context);
	}

	public InitView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initData(context);
	}

	public InitView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initData(context);
	}

	public void initData(final Context context) {
		airplane = new ImageButton(context);
		airplane.setBackgroundResource(R.drawable.airplane);
		airplane.setId(1);
		boat = new ImageButton(context);
		boat.setBackgroundResource(R.drawable.boat);
		boat.setId(2);
		tank = new ImageButton(context);
		tank.setBackgroundResource(R.drawable.tank);
		tank.setId(3);
		car = new ImageButton(context);
		car.setBackgroundResource(R.drawable.car);
		car.setId(4);
		airplane.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				context.startActivity(new Intent(context, MainActivity.class));
			}
		});
		boat.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {

			}
		});
		tank.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {

			}
		});
		car.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {

			}
		});
		this.addView(airplane);
		this.addView(boat);
		this.addView(tank);
		this.addView(car);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		int childCount = getChildCount();
		for (int i = 0; i < childCount; i++) {
			View child = getChildAt(i);
			child.setVisibility(View.VISIBLE);
			child.measure(r - l, b - t);
			int ww = getWidth() / 4;
			int hh = getHeight() / 4;
			switch (child.getId()) {
			case 1:
				child.layout(ww * 2 - child.getMeasuredWidth() / 2, hh - child.getMeasuredHeight() / 2, ww * 2 + child.getMeasuredWidth() / 2,
						hh + child.getMeasuredHeight() / 2);
				break;
			case 2:
				child.layout(ww * 3 - child.getMeasuredWidth() / 2, hh * 2 - child.getMeasuredHeight() / 2, ww * 3 + child.getMeasuredWidth() / 2,
						hh * 2 + child.getMeasuredHeight() / 2);
				break;
			case 3:
				child.layout(ww * 2 - child.getMeasuredWidth() / 2, hh * 3 - child.getMeasuredHeight() / 2, ww * 2 + child.getMeasuredWidth() / 2,
						hh * 3 + child.getMeasuredHeight() / 2);
				break;
			case 4:
				child.layout(ww * 1 - child.getMeasuredWidth() / 2, hh * 2 - child.getMeasuredHeight() / 2, ww * 1 + child.getMeasuredWidth() / 2,
						hh * 2 + child.getMeasuredHeight() / 2);
				break;
			default:
			}
		}
	}
}
